<!DOCTYPE html>
<html>
<head>
	<% include ../link %>
</head>
<body>
	<% include ../head %>
	<div class="row">
		<div class="col-md-8">
			<ul class="nav nav-pills" role="tablist">
				<li role="presentation"><a href="/admin/video">视频管理</a></li>
				<li role="presentation"><a href="/admin/article">文章管理</a></li>
				<li role="presentation"><a href="/admin/users">用户管理</a></li>
				<li role="presentation" class="active"><a href="#">添加管理员</a></li>
			</ul>
		</div>
		<div class="col-md-2">
			<input class="seid form-control" type="text" name="id" placeholder="id">
		</div>
		<div class="col-md-1">
			<button class="select button button-action button-rounded button-small">select</button>
		</div>
	</div>
	<table class="table table-striped">
		<thead>
			<tr>
				<th>id</th>
				<th>name</th>
				<th>motto</th>
				<th>img</th>
				<th>email</th>
				<th>regTime</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody data-bind="foreach: user">
			<tr>
				<th data-bind="text: id"></th>
				<th data-bind="text: name"></th>
				<th data-bind="text: motto"></th>
				<th data-bind="text: img"></th>
				<th data-bind="text: email"></th>
				<th data-bind="text: regTime"></th>
				<th>
					<button data-bind="attr: {title : id}" class="addadmin button button-caution button-rounded button-small">add</button>
				</th>
			</tr>
		</tbody>
	</table>
</body>
<script type="text/javascript">
	var user = {}
	var addadminBtn
	var selectBtn = $('.select');
	selectBtn.on('click',function(){
		if ($('.seid').val()!='') {
			var id = $('.seid').val();
			$.ajax({
				url:'/admin/selectUserone',
				type:'get',
				data:{
					'id':id
				},
				success:function(data){
					vm.user.push(new usersModel(data))
					setTimeout(function(){
						addadminBtn = $('.addadmin');
						fa()
					},500)
				}
			})
		} else {

		}
	})


	function getTime(time){
		time = parseInt(time);
		time = new Date(time).format("yyyy-MM-dd hh:ss");
		return time;
	}

	function ViewModel(){
		var self = this
		self.user = ko.observableArray()
	}

	var vm = new ViewModel();

	function usersModel(u){
		var self = this;
		self.id = u._id;
		self.name = u.name;
		self.motto = u.motto;
		self.img = u.headImgUrl;
		self.email = u.email;
		self.regTime = getTime(u.regTime);
		self.seal = u.seal;
	}

	ko.applyBindings(vm)

	function fa(){
		addadminBtn.on('click', function(){
			var id = $(this).attr('title');
			var btn = $(this)
			$.ajax({
				type:'put',
				url:'addadmin',
				data:{
					id:id
				},
				success:function(data){
					Messenger().post({
						message:data.dt,
						hideAfter:1
					});
					btn.parent().parent().remove();
				}
			})
		})
		
	}
</script>
</html>